﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using Microsoft.Phone.Tasks;
using Coding4Fun.Phone.Controls;

namespace MikeHole.TileMaker.TestApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        ObservableCollection<BitmapImage> myImages = new ObservableCollection<BitmapImage>();

        PhotoChooserTask photoChooser;

        ColorPicker colourPicker;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainPage_Loaded);

            photoChooser = new PhotoChooserTask();

            photoChooser.Completed += new EventHandler<PhotoResult>(photoChooser_Completed);

        }

        void photoChooser_Completed(object sender, PhotoResult e)
        {
            if( e.TaskResult == TaskResult.OK )
            {
                BitmapImage image = new BitmapImage();

                image.SetSource(e.ChosenPhoto);

                maker.SetTileImage( image );
            }
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            lstImages.ItemsSource = myImages;
        }

        private void cmdLoad_Click(object sender, EventArgs e)
        {
            photoChooser.Show();
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            BitmapImage bi = maker.GetCurrentTile();
            myImages.Add( bi );
        }

        private void cmdPickColor_Click(object sender, EventArgs e)
        {
            colourPicker = new ColorPicker();

            AboutPrompt p = new AboutPrompt();
            
            p.VersionNumber = "";

            p.Title = "Background";

            p.Body = colourPicker;

            p.Completed += new EventHandler<PopUpEventArgs<object, PopUpResult>>(p_Completed);

            p.Show();
        }

        void p_Completed(object sender, PopUpEventArgs<object, PopUpResult> e)
        {
            if(e.PopUpResult == PopUpResult.Ok)
                maker.SetBackground(colourPicker.SolidColorBrush);
        }
    }
}